package com.platform.qujia.controller;

import javax.validation.Valid;

import com.platform.qujia.constantEnum.TianQuetechPayEnum;
import com.platform.qujia.enums.CardRecordType;
import com.platform.qujia.enums.ElectronCardType;
import com.platform.qujia.enums.PhysicalCardPaymentEnum;
import com.platform.qujia.enums.PayTypeEnum;
import com.platform.qujia.param.request.*;
import com.platform.qujia.pojo.*;
import com.platform.qujia.pojo.dto.ScanOrderDTO;
import com.platform.qujia.pojo.vo.CardRechargeOrderPrintVO;
import com.platform.qujia.service.*;
import com.platform.qujia.utils.CodeMsg;
import com.platform.qujia.utils.MobilePhoneUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.platform.qujia.config.BasicInfo;
import com.platform.qujia.utils.ResultData;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.Date;
import java.util.Map;

@Controller
@RequestMapping("/payment")
@Api(tags = "支付")
public class PaymentController {

	
	@Autowired
	private IPaymentService paymentService;
	@Autowired
	private ElectronicCardService electronicCardService;
	@Autowired
	private PhysicalCardBalanceService physicalCardBalanceService;
	@Autowired
	private PhysicalCardService physicalCardService;


	/**
	 * 天阙支付-被扫支付
	 *
	 * @param request
	 * @param response
	 */
	@ApiOperation(value = "天阙支付-被扫支付")
	@PostMapping("/tradeReversePay")
	@ResponseBody
	public ResultData<TradeReversePayVO> tradeReversePay(@Valid @RequestBody AddPayOrderParam payParameter) {
		if (payParameter.getEnableDiscount() == null) {
			payParameter.setEnableDiscount(Boolean.TRUE);
		}
		TradeReversePayVO tradeReversePayVO = paymentService.scanPay2(payParameter);
		return ResultData.success(tradeReversePayVO);
	}

	@ApiOperation(value = "天阙支付-被扫支付")
	@PostMapping("/tradeReversePayN")
	@ResponseBody
	public ResultData<TradeReversePayVO> tradeReversePayN(@Valid @RequestBody AddPayOrderParam payParameter) throws Exception {
		if (payParameter.getEnableDiscount() == null) {
			payParameter.setEnableDiscount(Boolean.TRUE);
		}
		TradeReversePayVO tradeReversePayVO = paymentService.scanPay(payParameter);
		return ResultData.success(tradeReversePayVO);
	}

	@ApiOperation(value = "pos机-直接扫码支付")
	@PostMapping("/tradeReversePayPos")
	@ResponseBody
	public ResultData<TradeReversePayVO> tradeReversePayPos(@Valid @RequestBody AddPayOrderParamBase payParameter) {

		TradeReversePayVO tradeReversePayVO = paymentService.scanPayPos(payParameter);
		return ResultData.success(tradeReversePayVO);
	}


	@ApiOperation("电子卡pos扫码充值")
	@PostMapping("/dzk/posScanRecharge")
	@ResponseBody
	public ResultData<ScanOrderDTO> dzkPosScanRecharge(@RequestBody DzkPosRechargeReq req){
		ScanOrderDTO scanOrderDTO = electronicCardService.dzkScanRecharge(req);
		TianQuetechPayEnum status = scanOrderDTO.getStatus();
		if (status == TianQuetechPayEnum.PAY_SUCCESS) {
			return ResultData.success(scanOrderDTO);
		}else if(status == TianQuetechPayEnum.PAY_PAYING){
			return ResultData.error(CodeMsg.TRADE_REVERSE_PAY_WAIT_MINUTE.getRetCode(),CodeMsg.TRADE_REVERSE_PAY_WAIT_MINUTE.getMessage(),scanOrderDTO);
		}
		return ResultData.error(CodeMsg.PAY_FAIL.getRetCode(),CodeMsg.PAY_FAIL.getMessage(),scanOrderDTO);
	}

	@ApiOperation("电子卡pos扫码充值订单查询")
	@PostMapping("/dzk/queryRechargeOrder")
	@ResponseBody
	public ResultData<CustomerCardUseRecord> queryDzkRechargeOrder(@RequestBody TradeReverseScanPayParameter req){
		CustomerCardUseRecord useRecord = electronicCardService.queryDzkScanOrder(req);
		if (useRecord != null) {
			useRecord.setPayType(CardRecordType.getByKey(useRecord.getPayType(),CardRecordType.POS_RECHARGE).getName());
			useRecord.setCardOilsType(ElectronCardType.getByKey(useRecord.getCardOilsType(),ElectronCardType.GAS).getName()+"卡");
		}
		return ResultData.success(useRecord);
	}


	@ApiOperation("实体卡pos扫码充值")
	@PostMapping("/stk/posScanRecharge")
	@ResponseBody
	public ResultData<ScanOrderDTO> stkPosScanRecharge(@RequestBody PhysicalCardPosRechargeReq req){
		ScanOrderDTO scanOrderDTO = physicalCardService.physicalCardScanRecharge(req);
		TianQuetechPayEnum status = scanOrderDTO.getStatus();
		if (status == TianQuetechPayEnum.PAY_SUCCESS) {
			return ResultData.success(scanOrderDTO);
		}else if(status == TianQuetechPayEnum.PAY_PAYING){
			return ResultData.error(CodeMsg.TRADE_REVERSE_PAY_WAIT_MINUTE.getRetCode(),CodeMsg.TRADE_REVERSE_PAY_WAIT_MINUTE.getMessage(),scanOrderDTO);
		}
		return ResultData.error(CodeMsg.PAY_FAIL.getRetCode(),CodeMsg.PAY_FAIL.getMessage(),scanOrderDTO);
	}

	@ApiOperation("实体卡pos扫码充值订单查询")
	@PostMapping("/stk/queryRechargeOrder")
	@ResponseBody
	public ResultData<CardRechargeOrderPrintVO> queryStkRechargeOrder(@RequestBody TradeReverseScanPayParameter req){
		CardRechargeOrderPrintVO printVO = physicalCardService.queryPhysicalCardScanOrder(req);
		if (printVO != null) {
			printVO.setPayType(PhysicalCardPaymentEnum.getByValue(printVO.getPayType(),PhysicalCardPaymentEnum.POS).getText());
			printVO.setCardOilsType(ElectronCardType.getByKey(printVO.getCardOilsType(),ElectronCardType.GAS).getName()+"卡");
		}
		return ResultData.success(printVO);
	}

	@ApiOperation("实体卡pos扫码充值回调-随行付")
	@PostMapping("/stk/callback/sxf")
	@ResponseBody
	public String stkCallbackSxf(@RequestBody Map<String,Object> request){
		return physicalCardService.callbackSxf(request);
	}

	@ApiOperation("实体卡pos扫码充值回调-云化")
	@PostMapping("/stk/callback/yh")
	@ResponseBody
	public String stcCallbackYh(@RequestBody Map<String,Object> request){
		return physicalCardService.callbackYh(request);
	}


	/**
	 * 订单查询
	 * @param payParameter
	 * @return
	 */
	@ApiOperation(value = "订单查询")
	@PostMapping("/queryTradeReversePayOrder")
	@ResponseBody
	public ResultData<TradeReversePayVO> queryTradeReversePayOrder(@Valid @RequestBody AddPayOrderParam payParameter) {
		TradeReverseScanPayParameter parameter = new TradeReverseScanPayParameter();
		BeanUtils.copyProperties(payParameter,parameter);
		TradeReversePayVO vo = paymentService.queryOrder(parameter);
		PayOrder order = vo.getOrder();
		if (order != null) {
			String phone = order.getCustomerPhone();
			String encryPhone = MobilePhoneUtil.encryPhone(phone);
			order.setMobilePhone(encryPhone);
			if (order.getPayDate() == null) {
				order.setPayDate(new Date());
			}
		}
		return ResultData.success(vo);
	}
	
	
	/**
	 * 支付的商户编号查询
	 * @param payParameter
	 * @return
	 */
	@ApiOperation(value = "支付的商户编号查询")
	@PostMapping("/selectPayWayEnterpriseId")
	@ResponseBody
	public String selectPayWayEnterpriseId(@RequestBody QuestEnterpriseParam param) {
//		SelectPayWayEnterpriseVO vo=tradePaymentRecordsService.selectPayWayEnterpriseId(param);
		SelectPayWayEnterpriseVO vo=new SelectPayWayEnterpriseVO();
		PayWay payWay = paymentService.getStationPosPayWay(param.getStationId());
		vo.setEnterpriseId(param.getEnterpriseId());
		vo.setMno(payWay == null ? "" : payWay.getMno());
		vo.setOrgId(BasicInfo.TRADE_REVERSE_PAY_ORGID);
		return ResultData.successJson(vo);
	}
	
	
}
